#!/usr/bin/python

import unittest


from util.frequency import FreqControl


# Reference: https://www.cnblogs.com/yufeihlf/p/5752146.html
# Reference: http://pyunit.sourceforge.net/pyunit_cn.html
# appveyor: https://www.cnblogs.com/henryzhu/p/contentious-integration-with-appveyor.html
# appveyor: http://www.gulu-dev.com/post/2015-05-01-appveyor-ci
class FrequencyControlTestCase(unittest.TestCase):
    def setUp(self):
        self.st_ns_freq_control = FreqControl(
            thread_count=1,
            min_sleep_time=0,
            max_sleep_time=0
        )
        self.mt_ns_freq_control = FreqControl(
            thread_count=5,
            min_sleep_time=0,
            max_sleep_time=0
        )
        self.st_s_freq_control = FreqControl(
            thread_count=1,
            min_sleep_time=500,
            max_sleep_time=1500
        )
        self.mt_s_freq_control = FreqControl(
            thread_count=5,
            min_sleep_time=500,
            max_sleep_time=1500
        )


class SimpleFrequencyControlTestCase(FrequencyControlTestCase):
    def runTest(self):
        assert self.st_ns_freq_control.need_to_sleep, "Can't sleep"
        self.assertEqual(self.st_ns_freq_control.need_to_sleep, True)
        self.failUnlessEqual(self.st_ns_freq_control.need_to_sleep, True)


class Exception1FrequencyControlTestCase(FrequencyControlTestCase):
    def runTest(self):
        try:
            self.st_ns_freq_control.get_result(None)
        except ValueError:
            pass
        else:
            self.fail('expected a ValueError')


class Exception2FrequencyControlTestCase(FrequencyControlTestCase):
    def runTest(self):
        self.assertRaises(ValueError, self.st_ns_freq_control.get_result, None)
